package net.cooltain.warhammer.framework.entity;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;

import net.cooltain.utils.Logger;
import net.cooltain.warhammer.framework.constant.ConstantCardEdition;
import net.cooltain.warhammer.framework.constant.ConstantCardType;
import net.cooltain.warhammer.framework.constant.ConstantPhases;
import net.cooltain.warhammer.framework.constant.ConstantPicture;
import net.cooltain.warhammer.framework.constant.ConstantPlayZone;
import net.cooltain.warhammer.running.entity.MotionCard;
import net.cooltain.warhammer.running.entity.MotionPlayerZone;
import net.cooltain.warhammer.running.start.GameBoardDouble;
import net.cooltain.warhammer.running.window.popupItem.CannelAsAttactUnitItem;
import net.cooltain.warhammer.running.window.popupItem.CannelAsDefendUnitItem;
import net.cooltain.warhammer.running.window.popupItem.CardPopupItem;
import net.cooltain.warhammer.running.window.popupItem.ChooseAsAttactUnitItem;
import net.cooltain.warhammer.running.window.popupItem.ChooseAsDefendUnitItem;
import net.cooltain.warhammer.running.window.popupItem.PlayCardToBattleZoneItem;
import net.cooltain.warhammer.running.window.popupItem.PlayCardToKingdomZoneItem;
import net.cooltain.warhammer.running.window.popupItem.PlayCardToQuestZoneItem;
import net.cooltain.warhammer.running.window.popupItem.PlayDeveloperToBattleZoneItem;
import net.cooltain.warhammer.running.window.popupItem.PlayDeveloperToKingdomZoneItem;
import net.cooltain.warhammer.running.window.popupItem.PlayDeveloperToQuestZoneItem;

public class CardEntity {

	protected MotionPlayerZone player;
	
	protected MotionCard motionCard;
	
	protected String uniqueStr;//唯一标识
	
	protected Integer edition;//10集卡资讯
	
	protected Integer editionNumber;//10集卡资讯编号
	
	protected String teamLimit ;//阵营
	
	protected Integer type;//6 卡牌类型
	
	protected Integer race;//3 种族符号
	
	protected int cost;//2 费用
	
	protected int loyal;//4 忠诚图示
	
	protected int power;//5 能力图示
	
	protected int health;//7 生命值
	
	/** 是否所有玩家仅限一张,默认false */
	protected boolean allPlayerLimitOne = false; 
	
	/** 是否单个玩家仅限一张,默认false */
	protected boolean onePlayerLimitOne = false; 
	
	/** 是否单个区域仅限一张,默认false */
	protected boolean oneZoneLimitOne = false; 
	
	/** 限制放在哪个区域,默认ALL_ZONE_ALLOW,不限制区域 */
//	protected Integer zoneLimit = ConstantPlayZone.ALL_ZONE_ALLOW;
	protected boolean zoneBattleAllow  = true;
	protected boolean zoneKingdomAllow = true;
	protected boolean zoneQuestAllow   = true;
	protected boolean zoneCapitalAllow = false;
	
	//属性
	protected Integer[] traits ;
	
	//标题
	protected String cnName ;
	protected String enName ;
	
	//9卡牌规则
	protected String cnRules ;
	protected String enRules ;

	
	
	public String getUniqueStr() {
		return uniqueStr;
	}
	public void setUniqueStr(String uniqueStr) {
		this.uniqueStr = uniqueStr;
	}
	
	public Integer getEdition() {
		return edition;
	}
	public void setEdition(Integer edition) {
		this.edition = edition;
	}
	public Integer getEditionNumber() {
		return editionNumber;
	}
	public void setEditionNumber(Integer editionNumber) {
		this.editionNumber = editionNumber;
	}
	
	public String getTeamLimit() {
		return teamLimit;
	}
	public void setTeamLimit(String teamLimit) {
		this.teamLimit = teamLimit;
	}
	
	public int getCost() {
		return cost;
	}
	public void setCost(int cost) {
		this.cost = cost;
	}
	
	public Integer getRace() {
		return race;
	}
	public void setRace(Integer race) {
		this.race = race;
	}
	
	public int getLoyal() {
		return loyal;
	}
	public void setLoyal(int loyal) {
		this.loyal = loyal;
	}
	
	public int getPower() {
		return power;
	}
	public void setPower(int power) {
		this.power = power;
	}
	
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	
	public int getHealth() {
		return health;
	}
	public void setHealth(int health) {
		this.health = health;
	}
	
	public Integer[] getTraits() {
		return traits;
	}
	public void setTraits(Integer[] traits) {
		this.traits = traits;
	}

	public String getName(){
		return cnName;
	}
	public String getCnName() {
		return cnName;
	}
	public void setCnName(String cnName) {
		this.cnName = cnName;
	}
	public String getEnName() {
		return enName;
	}
	public void setEnName(String enName) {
		this.enName = enName;
	}
	
	
	public String getRules() {
		return cnRules;
	}
	public String getCnRules() {
		return cnRules;
	}
	public void setCnRules(String cnRules) {
		this.cnRules = cnRules;
	}
	public String getEnRules() {
		return enRules;
	}
	public void setEnRules(String enRules) {
		this.enRules = enRules;
	}
	
	
	public Boolean getAllPlayerLimitOne() {
		return allPlayerLimitOne;
	}
	public void setAllPlayerLimitOne(Boolean allPlayerLimitOne) {
		this.allPlayerLimitOne = allPlayerLimitOne;
	}
	public Boolean getOnePlayerLimitOne() {
		return onePlayerLimitOne;
	}
	public void setOnePlayerLimitOne(Boolean onePlayerLimitOne) {
		this.onePlayerLimitOne = onePlayerLimitOne;
	}
	public Boolean getOneZoneLimitOne() {
		return oneZoneLimitOne;
	}
	public void setOneZoneLimitOne(Boolean oneZoneLimitOne) {
		this.oneZoneLimitOne = oneZoneLimitOne;
	}
//	public Integer getZoneLimit() {
//		return zoneLimit;
//	}
//	public void setZoneLimit(Integer zoneLimit) {
//		this.zoneLimit = zoneLimit;
//	}
	
	public MotionPlayerZone getPlayer() {
		return player;
	}
	public void setPlayer(MotionPlayerZone player) {
		this.player = player;
	}
	
	/**
	 * 打出这张卡需要花费的资源数
	 * 当前印制费用+忠诚费用
	 * @return
	 */
	public int getNeedCost(){
		int needCost = this.motionCard.getCostCurrent() + this.motionCard.getCostRound() ;
		int raceFlag = this.player.getTeamRaceFlag( this.getRace() );
		if(this.getLoyal()>raceFlag){
			needCost += this.getLoyal() - raceFlag ;
		}
		return needCost ;
	}
	
	
	//==========================================================================
	// 构造方法
	/** 初始化一张卡牌 */
	public MotionCard initToMotion(MotionPlayerZone player){
		this.player = player;
		this.motionCard = new MotionCard();
		motionCard.player = player;
		initMotionParam();
		return motionCard ;
	}
	
	protected void initMotionParam(){
		motionCard.cardEntity = this;		
		motionCard.setCostCurrent( this.cost );
		motionCard.setLoyalCurrent(this.loyal);
		GameBoardDouble.geneterMotionCard(motionCard);
	}
	
	//==========================================================================

	public BufferedImage getBufferedImage() throws IOException{
		StringBuffer cardImagePath = new StringBuffer();
		cardImagePath.append(ConstantCardEdition.allEditionsPicPath.get(getEdition()));
		if(getEditionNumber()<100){
			cardImagePath.append(0);			
		}
		if(getEditionNumber()<10){
			cardImagePath.append(0);			
		}		
		cardImagePath.append(getEditionNumber());
		cardImagePath.append(ConstantPicture.DOT_JPG);
		BufferedImage cardImage = ImageIO.read(new File(cardImagePath.toString()));
		return cardImage;
	}
	
	public ImageIcon getIconImage() throws IOException{
		StringBuffer cardImagePath = new StringBuffer();
		cardImagePath.append(ConstantCardEdition.allEditionsPicPath.get(getEdition()));
		if(getEditionNumber()<100){
			cardImagePath.append(0);			
		}
		if(getEditionNumber()<10){
			cardImagePath.append(0);			
		}		
		cardImagePath.append(getEditionNumber());
		cardImagePath.append(ConstantPicture.DOT_JPG);		
		return new ImageIcon(cardImagePath.toString());
	}
	
	
	//==========================================================================
	// 供玩家调用的方法
	
	/**
	 * 做为设施卡打出
	 * @param player
	 * @param targetZone 目标区域
	 */
	public boolean playAsDeveloper(MotionPlayerZone player, MotionCard motionCard, Integer targetZone){
		if(!ConstantPlayZone.developerAllowZone.containsKey(targetZone)){
			if(player.isLogger){
				Logger.warn(new StringBuffer()
					.append(player.playerName).append("的")
					.append(this.getName())
					.append("不能做为设施卡被放置到 ")
					.append(ConstantPlayZone.developerAllowZone.get(targetZone))
				);
			}
			return false;
		}
		motionCard.setDevelopment( true );
		motionCard.setFlip(true);
		this.switchZone(targetZone);
		return true;
	}

	/**
	 * 
	 * @param targetZone
	 * @return
	 */
	public boolean switchZone(Integer targetZone){
		// 当前区域移出
		player.allZoneMap.get(this.motionCard.currentZone).eventRemoveCard(this.motionCard) ;
		// 新的区域增加
		player.allZoneMap.get(targetZone).eventAddCard(this.motionCard) ;
		return true;
	}
	
	/**
	 * 获得右键菜单
	 * @return
	 */
	public JPopupMenu genePopupMenu(MotionPlayerZone actPlayer){
		JPopupMenu jpopup = new JPopupMenu();
		int menuSize = 0;
		// 在[我的回合]的[首都阶段]从[手牌]打出
		if( this.player.isMyTurn 
			&& this.player.currentPhases==ConstantPhases.CAPITAL_PHASES
			&& this.motionCard.currentZone==ConstantPlayZone.HAND_ZONE ){
					
			if(this.getType() == ConstantCardType.CARD_TYPE_UNIT 
				||this.getType() == ConstantCardType.CARD_TYPE_SUPPORT
				||this.getType() == ConstantCardType.CARD_TYPE_QUEST
				||this.getType() == ConstantCardType.CARD_TYPE_LEGEND  ){
				
				boolean isSeparator = false;
				if(this.zoneBattleAllow){
					jpopup.add(new PlayCardToBattleZoneItem(this.motionCard ));
					menuSize++;
					isSeparator = true;
				}
				if(this.zoneKingdomAllow){
					jpopup.add(new PlayCardToKingdomZoneItem(this.motionCard));
					menuSize++;
					isSeparator = true;
				}
				if(this.zoneQuestAllow){
					jpopup.add(new PlayCardToQuestZoneItem(this.motionCard));
					menuSize++;
					isSeparator = true;
				}
				if(this.zoneCapitalAllow){
					jpopup.add(new CardPopupItem("打到首都区",this.motionCard));
					menuSize++;
					isSeparator = true;
				}
				if(isSeparator){
					jpopup.addSeparator();
				}
			}
		}//~在[我的回合]的[首都阶段]从[手牌]打出卡片
		
		// 允许打出设施卡的阶段
		if( ( this.player.allowDeveloperMyTurn 
			&& this.player.allowDeveloperSet>0 
			&& this.player.allowDeveloperPhases.get(this.player.currentPhases))
			|| (this.player.allowDeveloperOperateTurn
			&& this.player.allowDeveloperSet>0 
			&& this.player.allowDeveloperPhases.get(this.player.operatePlayer.currentPhases))){
			jpopup.add(new PlayDeveloperToBattleZoneItem(this.motionCard));
			jpopup.add(new PlayDeveloperToKingdomZoneItem(this.motionCard));
			jpopup.add(new PlayDeveloperToQuestZoneItem(this.motionCard));
			menuSize +=3;
		}
		
		
		// 选为进攻部队
		if( this.player.isMyTurn 
			&& this.player.currentPhases == ConstantPhases.BATTLEFIELD_PHASES ){
			jpopup.add(new ChooseAsAttactUnitItem(motionCard));
			jpopup.add(new CannelAsAttactUnitItem(motionCard));
		}
		
		// 选为守部队
		if( this.player.operatePlayer.isMyTurn 
				&& this.player.operatePlayer.currentPhases == ConstantPhases.BATTLEFIELD_PHASES ){
			jpopup.add(new ChooseAsDefendUnitItem(motionCard));
			jpopup.add(new CannelAsDefendUnitItem(motionCard));
		}
		
		// TODO 卡片自带效果
		
		if(menuSize>0){
			return jpopup; 
		}else{
			return null;
		}
	}
	
}
